<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="520px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="收支类型">
        <el-radio-group v-model="form.bill_type">
          <el-radio label="income">收入</el-radio>
          <el-radio label="payed">支出</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="费用名称" prop="bill_title">
        <el-input v-model="form.bill_title" placeholder="请输入费用名称" />
      </el-form-item>
      <el-form-item label="账单金额" prop="bill_money">
        <el-input v-model="form.bill_money" placeholder="请输入账单金额" />
      </el-form-item>
      <el-form-item label="日期" prop="bill_date">
        <el-date-picker
          v-model="form.bill_date"
          format="yyyy-MM-dd"
          placeholder="请选择记账日期"
          type="date"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="备注" prop="bill_desc">
        <el-input
          v-model="form.bill_desc"
          maxlength="150"
          placeholder="请填写备注信息"
          :rows="4"
          show-word-limit
          type="textarea"
        />
      </el-form-item>
      <el-form-item label="图片凭证" prop="bill_images">
        <images-upload
          ref="imagesUpload"
          :image="form.bill_images"
          :limit="3"
          :max="10"
          @onChange="getImages"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addBill } from '@/api/bill'
  import { addOwnerBill } from '@/api/ownerBill'
  import { parseTime } from '@/utils/index'
  import imagesUpload from '@/components/imagesUpload'
  export default {
    name: 'DidBill',
    components: { imagesUpload },
    data() {
      return {
        type: '',
        form: {
          bill_type: 'income',
          bill_title: '',
          bill_money: '',
          is_pay: '1',
          bill_date: parseTime(new Date(), '{y}-{m}-{d}'),
          bill_images: '',
          bill_desc: '',
        },
        rules: {
          bill_title: [
            { required: true, trigger: 'blur', message: '请输入费用名称' },
          ],
          bill_money: [
            { required: true, trigger: 'blur', message: '请输入账单金额' },
          ],
          bill_date: [
            { required: true, trigger: 'blur', message: '请选择记账日期' },
          ],
        },
        title: '添加记账',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(type, row) {
        console.log(type, row)
        if (type == 'rent') {
          this.form.rent_sn = row.rent_sn
        } else if (type == 'owner') {
          this.form.owner_id = row.id
          this.form.room_id = row.room_id
        }
        this.type = type
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      getImages(e) {
        this.form.bill_images = e.join(',')
      },
      save() {
        let e = Object.assign({}, this.form)
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.type == 'rent') {
              const { data } = await addBill(e)
              if (data.is_success) {
                this.$baseMessage(
                  '添加成功',
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              }
            } else if (this.type == 'owner') {
              const { data } = await addOwnerBill(e)
              if (data.is_success) {
                this.$baseMessage(
                  '添加成功',
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              }
            }
          }
        })
      },
    },
  }
</script>
